package com.zx.tradeclient.ctrl.index5;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.zx.base.ctrl.Constant;
import com.zx.base.http.HttpUtils;
import com.zx.base.http.HttpUtils.CallBack;
import com.zx.base.lazylist.ImageLoader;
import com.zx.base.widget.LoadMoreListView;
import com.zx.base.widget.LoadMoreListView.OnLoadMoreListener;
import com.zx.base.widget.LoadMoreListViewWithProgress;
import com.zx.tradeclient.R;
import com.zx.tradeclient.app.TradeApp;
import com.zx.tradeclient.ctrl.index2.OrderInfoActivity;
import com.zx.tradeclient.entity.OrderInfo;

public class BuyerOrderList {

	private NewsListAdapter newsListAdapter;
	private LoadMoreListViewWithProgress news_view;
	private LoadMoreListView news_listView;
	private ArrayList<OrderInfo> newsList = new ArrayList<OrderInfo>();
	private int page;
	private TradeApp app;
	private ImageLoader imageLoader;
	private Context content;

	public BuyerOrderList(final Context context, View view) {
		content = context;
		page = 0;
		imageLoader = ((TradeApp) context.getApplicationContext()).imageLoader;
		init();
		initview();
		app = (TradeApp) context.getApplicationContext();
		news_view = (LoadMoreListViewWithProgress) view.findViewById(R.id.list);
		news_listView = news_view.getListView();
		newsListAdapter = new NewsListAdapter(context, newsList);
		news_listView.setAdapter(newsListAdapter);
		news_listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, final View view, int position, long id) {
				 Intent intent=new Intent(context,OrderInfoActivity.class);
				 intent.putExtra("orderID",""+newsList.get(position).getId());
				 content.startActivity(intent);
			}
		});
		news_listView.setOnLoadMoreListener(new OnLoadMoreListener() {

			@Override
			public void onLoadMore() {
				// TODO 拖动刷新
				page++;
				goodsListTask();

			}
		});
		goodsListTask();
	}

	private void init() {
		page = 1;
	}

	private void initview() {

	}

	private static class ViewHolder {
		public TextView orderId;
		public TextView orderState;
		public TextView buyTime;
		public TextView totalMoney;
	}

	private class NewsListAdapter extends BaseAdapter {

		private List<OrderInfo> topics;
		private LayoutInflater mlinflater;

		public NewsListAdapter(Context context, List<OrderInfo> list) {
			this.mlinflater = LayoutInflater.from(context);
			this.topics = list;
		}

		@Override
		public int getCount() {
			return topics.size();
		}

		@Override
		public Object getItem(int position) {
			return Integer.valueOf(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(final int position, View view, ViewGroup viewGroup) {
			final OrderInfo topic = topics.get(position);
			if (view == null) {
				view = mlinflater.inflate(R.layout.order_list_item, null);
				ViewHolder vh = new ViewHolder();
				vh.orderId = (TextView) view.findViewById(R.id.list_order_id);
				vh.orderState = (TextView) view.findViewById(R.id.list_order_state);
				vh.totalMoney = (TextView) view.findViewById(R.id.list_order_price);
				vh.buyTime = (TextView) view.findViewById(R.id.list_order_creattime);
				view.setTag(vh);
			}
			ViewHolder holder = (ViewHolder) view.getTag();
			holder.orderId.setText(topic.getOrderId());
			holder.orderState.setText(topic.getOrderState());
			holder.totalMoney.setText(topic.getTotalMoney());
			holder.buyTime.setText(topic.getBuyTime());

			return view;
		}
	}

	private void goodsListTask() {

		Map<String, String> map = new HashMap<String, String>();
		map.put("act", "orderFormList");
		map.put("userId", app.user.getId());
		map.put("type", "1");
		map.put("currentPage", "" + page);
		map.put("rowCountPerPage", "10");
		HttpUtils.post(map, new CallBack() {

			@Override
			public void onSuccess(JSONObject response) {
				parseJson(response);

			}

			@Override
			public void onFailure(Throwable e) {
				Toast toast = Toast.makeText(content, "网络超时。", Toast.LENGTH_SHORT);
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
			}
		});
	}

	private void parseJson(JSONObject response) {
		try {
			String currentPage = response.getString("currentPage");
			String totalPage = response.getString("totalPage");
			if (Constant.debug)
				Log.d("stop", currentPage);
			if (Constant.debug)
				Log.d("stop", totalPage);
			if (currentPage.equals(totalPage)) {
				news_listView.loadComplete(false);
			}
			JSONArray list = new JSONArray(response.getString("dataList"));
			for (int i = 0; i < list.length(); i++) {
				OrderInfo one = new OrderInfo();
				JSONObject goods = list.getJSONObject(i);
				one.setOrderId(goods.getString("orderid"));
				one.setOrderState(goods.getString("orderstatus"));
				one.setBuyTime(goods.getString("buytimer"));
				one.setTotalMoney(goods.getString("totalmoney"));
				one.setId(goods.getString("id"));
				newsList.add(one);
			}
			newsListAdapter.notifyDataSetChanged();
			news_view.show();
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

}
